﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.DependencyInjection/AppAutofacServiceProviderFactory 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       AppAutofacServiceProviderFactory
* 创建时间：  2025/6/3 16:51:03
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Autofac.Builder;
using Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.DependencyInjection
{

    public class AppAutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder>
    {
        private readonly Action<ContainerBuilder> _configurationAction;

        private readonly ContainerBuildOptions _containerBuildOptions;

        public AppAutofacServiceProviderFactory(ContainerBuildOptions containerBuildOptions, Action<ContainerBuilder> configurationAction = null)
            : this(configurationAction)
        {
            _containerBuildOptions = containerBuildOptions;
        }

        public AppAutofacServiceProviderFactory(Action<ContainerBuilder> configurationAction = null)
        {
            _configurationAction = configurationAction ?? ((Action<ContainerBuilder>)delegate
            {
            });
        }

        public ContainerBuilder CreateBuilder(IServiceCollection services)
        {
            ContainerBuilder containerBuilder = new ContainerBuilder();
            containerBuilder.Populate(services);
            _configurationAction(containerBuilder);
            return containerBuilder;
        }

        public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder)
        {
            if (containerBuilder == null)
            {
                throw new ArgumentNullException("containerBuilder");
            }

            return new AutofacServiceProvider(containerBuilder.Build(_containerBuildOptions));
        }
    }
}
